<template>
  <section>
		<!--工具条-->
		<el-col :span="24" class="toolbar" style="padding-bottom: 0px;">
			<el-form :inline="true" :model="query">
				<el-form-item>
					<el-input v-model="query.keyword" placeholder="关键字"></el-input>
				</el-form-item>
				<el-form-item>
					<el-button type="primary" v-on:click="search">查询</el-button>
				</el-form-item>
				<el-form-item>
					<el-button type="primary" @click="handleAdd">新增</el-button>
				</el-form-item>
			</el-form>
		</el-col>

    <!--列表-->
    <el-table :data="pageInfo.rows" highlight-current-row v-loading="listLoading" @selection-change="selsChange" style="width: 100%;">
      <el-table-column type="selection" width="55">
      </el-table-column>
      <el-table-column type="index" width="60">
      </el-table-column>
      <el-table-column prop="sn" label="Sn" width="150">
      </el-table-column>
      <el-table-column prop="title" label="标题" width="200">
      </el-table-column>
      <el-table-column prop="intro" label="描述" width="300">
      </el-table-column>
      <el-table-column label="操作" width="350">
        <template scope="scope">
          <el-button size="small" @click="handleEdit(scope.$index, scope.row)">编辑</el-button>
          <el-button type="danger" size="small" @click="handleDel(scope.$index, scope.row)">删除</el-button>

        </template>
      </el-table-column>
    </el-table>

    <!--工具条-->
    <el-col :span="24" class="toolbar">
      <el-button type="danger" @click="batchRemove" :disabled="this.sels.length===0">批量删除</el-button>
      <el-pagination
          @size-change="handleSizeChange"
          @current-change="handleCurrentChange"
          :current-page="query.currentPage"
          :page-sizes="[5, 10, 15, 20]"
          :page-size="query.pageSize"
          layout="total, sizes, prev, pager, next, jumper"
          :total="pageInfo.total" style="float:right;">
      </el-pagination>
    </el-col>


		<!--新增界面-->
		<el-dialog title="新增/修改" :visible.sync="addFormVisible" :close-on-click-modal="false">
			<el-form :model="addForm" label-width="80px" :rules="addFormRules" ref="addForm">
        <el-form-item label="SN"   >
          <el-input v-model="addForm.sn" auto-complete="off"></el-input>
        </el-form-item>
        <el-form-item label="Value" >
          <el-input v-model="addForm.title" auto-complete="off"></el-input>
        </el-form-item>
        <el-form-item label="intro" >
          <el-input v-model="addForm.intro" auto-complete="off"></el-input>
        </el-form-item>
        
			</el-form>
			<div slot="footer" class="dialog-footer">
				<el-button @click.native="addFormVisible = false">取消</el-button>
				<el-button type="primary" @click.native="addSubmit" :loading="addLoading">提交</el-button>
			</div>
		</el-dialog>

    </section>
</template>


<script>
	import util from '../../common/js/util'
	//import NProgress from 'nprogress'
	import { getUserListPage, removeUser, batchRemoveUser, editUser, addUser } from '../../api/api';

	export default {
		data() {
			return {
        dictionarys:[],

				filters: {
					name: ''
				},
        pageInfo:{
          total:0,
          rows:[]
        },
        query:{
          currentPage:1,
          pageSize:10,
          keyword:""
        },
				listLoading: false,
				sels: [],//列表选中列

				addFormVisible: false,//新增界面是否显示
				addLoading: false,
				addFormRules: {
					name: [
						{ required: true, message: '请输入信息', trigger: 'blur' }
					]
				},
				//新增界面数据
				addForm: {
          id:"",
          sn:"",
          title:"",
          intro:""

        },


        }


		},
		methods: {
      
      getALLdictionary(){
        this.$http.get("/dictionary")
            .then(res =>{
              res=res.data;
              console.log(res.data)
              if (res.success){
                this.dictionarys=res.data;
              }
            })
            .catch(res =>{


            })

      },


		  search(){
        this.query.currentPage = 1;
        this.getdictionary()
      },

      handleCurrentChange(val) {
        this.query.currentPage = val;
        this.getdictionary()
      },

      handleSizeChange(val){
        this.query.pageSize=val;
        this.getdictionary()
      },

      getdictionary(){
        this.$http.post("/dictionary",this.query)
            .then(result => {
              result = result.data;
              this.pageInfo = result.data;
              console.log(result.data)
            }).catch(result => {
          this.$message({ message: '员工分页查询失败',type: 'error'});
        })
      },

			//删除  index:当前数据的行号    row:当前行数据
			handleDel: function (index, row) {
				this.$confirm('确认删除该记录吗?','提示',{
					type: 'warning'
				}).then(() => {
					this.listLoading = true;  //加载框 true-加载

          let id=row.id;
          this.$http.delete("/dictionary/"+id).then(res =>{
            this.listLoading=false;
            res=res.data;
            if(res.success){
              this.$message({message: '删除成功', type: 'success'});
            }else {
              this.$message({message:res.message, type: 'error'});
            }
            this.query.currentPage=1
            this.getdictionary()
          })

				}).catch(() => {
              this.$message('删除操作已取消')
				});
			},
			//显示编辑界面
      handleEdit: function (index,row) {
        this.addFormVisible = true;
        this.getALLdictionary();
        //将当前对象赋值给addform  回显
        this.addForm=Object.assign({},row);
        if(!row.parent){
          this.addForm.parent={
            id:null,
            name:"",
          };

        }



      },
			//显示新增界面
			handleAdd: function () {
				this.addFormVisible = true;
				this.getALLdictionary();
				this.addForm = {
          id:null,
          name:"",
          url:"",
          icon:"",
          parent:{
            id:null,
            name:"",
          },
				};
			},

			//新增
			addSubmit: function () {
				this.$refs.addForm.validate((valid) => {
					if (valid) {
              this.$confirm('确定提交？','提示',{}).then(() =>{
                 this.addLoading=true;
                 let para = Object.assign({},this.addForm);


                  this.$http.put("/dictionary",para).then(res =>{
                     res=res.data;
                     this.addLoading=false;
                     if(res.success){
                       this.$message({message: '保存成功', type: 'success'});
                       this.query.currentPage=1;
                       this.getdictionary();
                       this.addFormVisible=false;
                     }else {
                       this.$message({message: res.message, type: 'error'});
                     }
                  }).catch(res =>{
                    this.$message({message: '网络错误', type: 'error'});
                    this.addLoading=false;
                  })
              })
					}
				});
			},
			selsChange: function (sels) {
				this.sels = sels;
			},
			//批量删除
			batchRemove: function () {
				var ids = this.sels.map(item => item.id)
        console.log(ids)
				this.$confirm('确认删除选中记录吗？', '提示', {
					type: 'warning'
				}).then(() => {
					this.listLoading = true;
					this.$http.patch("/dictionary",ids).then(res =>{
					  res= res.data;
					  if(res.success){
              this.$message({message: '删除成功', type: 'success'});
              this.listLoading=false;
              this.query.currentPage=1;
              this.getdictionary()
					  }else {
              this.$message({message: res.message, type: 'error'});
            }
          })


				}).catch(() => {
          this.$message({message: '取消删除', type: 'error'});
				});
			}
		},


		mounted() {
      this.getdictionary()
		}
	}

</script>

<style scoped>

</style>